local skill = fk.CreateSkill {
  name = "iberia__archery_attack_skill",
}

local I = require "packages/iberia/utility"

Fk:loadTranslationTable{
  ["#iberia__archery_attack-cancel"] = "折冲：可以为 %arg 取消一名目标",
}

skill:addEffect("cardskill", {
  prompt = "#archery_attack_skill",
  can_use = Util.AoeCanUse,
  mod_target_filter = function(self, player, to_select)
    return to_select ~= player
  end,
  on_use = function (self, room, cardUseEvent)
    local player = cardUseEvent.from
    local tos = #cardUseEvent.tos > 0 and cardUseEvent.tos or table.filter(room.alive_players, function (p)
      return p ~= player and not player:isProhibited(p, cardUseEvent.card)
    end)

    if I.isRepeller(player) then
      local to = room:askToChoosePlayers(player, {
        max_num = 1,
        min_num = 1,
        targets = tos,
        skill_name = "iberia__archery_attack",
        cancelable = true,
        prompt = "#iberia__archery_attack-cancel:::" .. cardUseEvent.card:toLogString()
      })

      if #to == 1 then
        table.removeOne(tos, to[1])
      end
    end

    if not cardUseEvent.tos or #cardUseEvent.tos == 0 then
      cardUseEvent.tos = tos
    end
  end,
  on_effect = function(self, room, effect)
    local loopTimes = effect:getResponseTimes()
    local respond
    for _ = 1, loopTimes do
      local params = { ---@type AskToUseCardParams
        skill_name = 'jink',
        pattern = 'jink',
        cancelable = true,
        event_data = effect
      }
      respond = room:askToResponse(effect.to, params)

      if respond then
        room:responseCard(respond)
      else
        room:damage({
          from = effect.from,
          to = effect.to,
          card = effect.card,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = "iberia__archery_attack",
        })
      end

      if effect.to.dead then break end
    end
  end,
})

return skill
